package com.mi.hospital;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class hospital {

    public static class areaMapper extends Mapper<LongWritable, Text, Text, FloatWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            if (key.get() == 0) return;
            String line = value.toString();
            String[] fields = line.split(",");
            // 医院编码_NN
            String hospitalId = fields[20];
            // 治疗费发生金额_SUM
            String treatmentCosts = fields[40];
            // 输出
            context.write(new Text(hospitalId), new FloatWritable(Float.parseFloat(treatmentCosts)));
        }
    }

    public static class areaReducer extends Reducer<Text, FloatWritable, Text, FloatWritable> {
        private final Map<String, Float> treatmentCostsMap = new HashMap<>();

        @Override
        protected void reduce(Text key, Iterable<FloatWritable> values, Context context) throws IOException, InterruptedException {
            float sum = 0;
            int count = 0;
            for (FloatWritable val : values) {
                sum += val.get();
                count++;
            }
            float avg = sum / count;
            treatmentCostsMap.put(key.toString(), avg);
        }

        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {
            ArrayList<Map.Entry<String, Float>> list = new ArrayList<>(treatmentCostsMap.entrySet());
            list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
            for (Map.Entry<String, Float> treatmentCosts : list) {
                context.write(new Text("医院 " + treatmentCosts.getKey() + " 人均总治疗费用"), new FloatWritable(treatmentCosts.getValue()));
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        // 创建配置类
        Configuration config = new Configuration();
        // 创建Job作业对象
        Job job = Job.getInstance(config);
        // 指定Job对象所属的类
        job.setJarByClass(hospital.class);
        // 指定Mapper
        job.setMapperClass(areaMapper.class);
        // 指定Mapper输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(FloatWritable.class);
        // 指定ReducergetMapOutputValueClass
        job.setReducerClass(areaReducer.class);
        // 指定Reducer输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FloatWritable.class);
        FileInputFormat.setInputPaths(job, new Path("data/data.csv"));
        // 指定处理结果的输出路径
        Path outputPath = new Path("output");
        FileSystem fs = FileSystem.get(config);
        if (fs.exists(outputPath)) {
            fs.delete(outputPath, true);
        }
        FileOutputFormat.setOutputPath(job, outputPath);
        // 提交作业
        boolean success = job.waitForCompletion(true);
        if (success) {
            System.out.println("执行成功");
        } else {
            System.out.println("执行失败");
        }
    }

}
